﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarolLib.Control;
using System.Xml;
using System.Collections;
using DispatchAssist.Service;
using System.Web.Services.Protocols;

namespace DispatchAssist.AppHost
{
    [ModuleContract("FEA0610F-EF06-4820-8D38-13D4FFD7C75A", "系统管理", "预警时间设置")]
    public partial class EarlyWarningManage : MdiUserControl
    {
        RuleManagerService service = new RuleManagerService();
        List<EarlyWarning> earlyWarningLst = null;
        int previewIndex = 0;
        public EarlyWarningManage()
            : base()
        {
            InitializeComponent();
        }
        public EarlyWarningManage(XmlNode xn)
            : this()
        {
        }



        private void BindDataGrid()
        {
            try
            {
                earlyWarningLst = service.GetAllEarlyWarningRecords().ToList<EarlyWarning>();
                dgEarlyWarningList.Rows.Clear(); ;
                int i = 0;
                foreach (EarlyWarning ew in earlyWarningLst)
                {

                    DataGridViewRow row = new DataGridViewRow();
                    dgEarlyWarningList.Rows.Add(row);
                    dgEarlyWarningList.Rows[i].Cells[0].Value = ew.EarlyWarningID;

                    if (ew.Airport == "0000") dgEarlyWarningList.Rows[i].Cells[1].Value = "通用规则机场";
                    else dgEarlyWarningList.Rows[i].Cells[1].Value = ew.Airport;

                    dgEarlyWarningList.Rows[i].Cells[2].Value = ew.AOCEarlyTime;
                    dgEarlyWarningList.Rows[i].Cells[3].Value = ew.SMSEarlyTime;
                    i++;
                }
            }
            catch (SoapHeaderException soapEx)
            {
                MessageBox.Show("SoapHeader Error : " + soapEx.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }

        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            //service.
            try
            {
                string tagstr = (sender as Button).Tag.ToString();
                List<EarlyWarning> lst = service.GetAllEarlyWarningRecords().ToList<EarlyWarning>();

                EarlyWarning ew = lst.Find(s => s.Airport == ((txtAirPortCode.Text == "通用规则机场") ? "0000" : txtAirPortCode.Text));

                List<InfoAirport> lstAirports = service.GetAllAirports().ToList<InfoAirport>();

                InfoAirport airport = lstAirports.Find(s => s.Code3 == txtAirPortCode.Text);

                if (string.IsNullOrEmpty(txtAirPortCode.Text))
                {
                    MessageBox.Show("请输入机场编码！");
                    txtAirPortCode.Focus();
                    return;
                }

                if (airport == null && txtAirPortCode.Text != "通用规则机场")
                {
                    MessageBox.Show("输入的机场编码不正确！");
                    txtAirPortCode.Focus();
                    return;
                }

                if (string.IsNullOrEmpty(txtAOCEarlyTime.Text))
                {
                    MessageBox.Show("请输入AOC提醒时间！");
                    txtAOCEarlyTime.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(txtSMSEarlyTime.Text))
                {
                    MessageBox.Show("请输入SMS提醒时间！");
                    txtSMSEarlyTime.Focus();
                    return;
                }

                // 提前时间不能超过一个合理的限度, 目前根据chen peiyu建议, 设置为6小时、8小时.
                // ccp, 2011.8.16
                if (int.Parse(txtAOCEarlyTime.Text) > 6)
                {
                    MessageBox.Show("AOC提醒时间超出6小时范围！");
                    txtAOCEarlyTime.Text = "";
                    txtAOCEarlyTime.Focus();
                    return;

                }
                if (int.Parse(txtSMSEarlyTime.Text) > 8)
                {
                    MessageBox.Show("SMS提醒时间超出8小时范围！");
                    txtSMSEarlyTime.Text = "";
                    txtSMSEarlyTime.Focus();
                    return;
                }




                EarlyWarning earlyWarning = null;
                if (tagstr == "create")
                {
                    if (ew != null)
                    {
                        MessageBox.Show("机场编码已经存在！");
                        return;
                    }
                    earlyWarning = new EarlyWarning();

                }
                else if (tagstr == "alter")
                {
                    earlyWarning = lst.Find(s => s.EarlyWarningID == ((Guid)(dgEarlyWarningList.CurrentRow.Cells[0].Value)));
                    if ((earlyWarning.Airport != ((txtAirPortCode.Text == "通用规则机场") ? "0000" : txtAirPortCode.Text)) && ew != null)
                    {
                        MessageBox.Show("机场编码已经存在！");
                        return;
                    }
                }

                earlyWarning.Airport = (txtAirPortCode.Text == "通用规则机场") ? "0000" : txtAirPortCode.Text;
                earlyWarning.AOCEarlyTime = short.Parse(txtAOCEarlyTime.Text);
                earlyWarning.SMSEarlyTime = short.Parse(txtSMSEarlyTime.Text);

                earlyWarning.LastModifyTime = DateTime.Now;


                if (tagstr == "create")
                {
                    if (service.CreateEarlyWarning(earlyWarning))
                    {
                        MessageBox.Show("添加成功！");
                        grpEarlyWarningInfo.Visible = false;
                        BindDataGrid();

                        dgEarlyWarningList.Rows[0].Selected = true;
                        dgEarlyWarningList.CurrentCell = dgEarlyWarningList.Rows[0].Cells[1];
                        dgEarlyWarningList_SelectionChanged(null, null);


                        setControlsEnabled(false);

                        btnEdit.Enabled = true;
                        btnDelete.Enabled = true;
                        btnCreate.Enabled = true;
                    }
                    else
                    {
                        MessageBox.Show("保存失败！");
                    }

                }
                else if (tagstr == "alter")
                {
                    if (service.UpdateEarlyWarning(earlyWarning))
                    {
                        MessageBox.Show("修改成功！");
                        grpEarlyWarningInfo.Visible = false;
                        int indexx = dgEarlyWarningList.CurrentRow.Index;
                        BindDataGrid();
                        dgEarlyWarningList.Rows[indexx].Selected = true;
                        dgEarlyWarningList.CurrentCell = dgEarlyWarningList.Rows[indexx].Cells[1];
                        dgEarlyWarningList_SelectionChanged(null, null);
                        setControlsEnabled(false);
                    }
                    else
                    {
                        MessageBox.Show("保存失败！");
                    }
                }
            }
            catch (SoapHeaderException soapEx)
            {
                MessageBox.Show("SoapHeader Error : " + soapEx.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }

        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            btnSave.Tag = "create";
            setControlsEnabled(true);
            setControlsEmpty();
            grpEarlyWarningInfo.Visible = true;

            btnEdit.Enabled = false;
            btnDelete.Enabled = false;
            btnCreate.Enabled = false;
            txtAirPortCode.Enabled = true;
            txtAirPortCode.Focus();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btnSave.Tag = "alter";
            setControlsEnabled(true);
            grpEarlyWarningInfo.Visible = true;


            btnEdit.Enabled = false;
            btnDelete.Enabled = false;
            btnCreate.Enabled = false;

        }

        private void setControlsEnabled(bool boo)
        {

            btnSave.Enabled = boo;
            btnCancel.Enabled = boo;

            //dgEarlyWarningList.Enabled = !boo;

        }
        private void setBtnEnabled(bool boo)
        {
            btnDelete.Enabled = boo;
            btnCreate.Enabled = boo;
            btnEdit.Enabled = boo;
        }

        private void setControlsEmpty()
        {
            txtAirPortCode.Text = "";
            txtAirPortCode.Focus();
            txtAOCEarlyTime.Text = "";
            txtSMSEarlyTime.Text = "";
        }
        private void EarlyWarningManage_Load(object sender, EventArgs e)
        {

        }

        public override void Onload()
        {
            service.DASoapHeaderValue = AuthContext.Current.GetSoapHeader();

            dgEarlyWarningList.MultiSelect = false;
            grpEarlyWarningInfo.Visible = false;
            BindDataGrid();
            setControlsEnabled(false);

            base.Onload();
        }

        private void dgEarlyWarningList_SelectionChanged(object sender, EventArgs e)
        {
            if (dgEarlyWarningList.Rows.Count == 0) return;

            int index = dgEarlyWarningList.CurrentRow.Index;
            EarlyWarning earlyWarning = new EarlyWarning();
            bool flag = false;
            if (dgEarlyWarningList.Rows[index].Cells[0].Value == null) { if (earlyWarningLst.Count > 0) { if (index > earlyWarningLst.Count - 1) { earlyWarning = new EarlyWarning(); setBtnEnabled(false); } else { earlyWarning = earlyWarningLst[0]; setBtnEnabled(true); } } else { setBtnEnabled(true); return; } }
            else
            {
                Guid selectedID;

                if ((grpEarlyWarningInfo.Visible == true))
                {
                    if (MessageBox.Show("是否确定撤销当前编辑？", "确认框", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        grpEarlyWarningInfo.Visible = false;
                        btnCancel.Tag = "none";
                        btnEdit.Enabled = true;
                        btnDelete.Enabled = true;
                        btnCreate.Enabled = true;
                        selectedID = (Guid)dgEarlyWarningList.Rows[index].Cells[0].Value;
                        dgEarlyWarningList.Rows[index].Selected = true;
                        setBtnEnabled(true);
                    }
                    else
                    {

                        grpEarlyWarningInfo.Visible = false;
                        selectedID = (Guid)dgEarlyWarningList.Rows[previewIndex].Cells[0].Value;
                        dgEarlyWarningList.Rows[previewIndex].Selected = true;
                        dgEarlyWarningList.CurrentCell = dgEarlyWarningList.Rows[previewIndex].Cells[1];
                        dgEarlyWarningList.ClearSelection();
                        grpEarlyWarningInfo.Visible = true;
                        setBtnEnabled(false);

                        if (btnSave.Tag.ToString() == "create")
                        {
                            flag = true;
                            btnCreate_Click(null, null);
                        }


                    }

                }
                else
                {
                    selectedID = (Guid)dgEarlyWarningList.Rows[index].Cells[0].Value;
                    dgEarlyWarningList.Rows[index].Selected = true;
                    setBtnEnabled(true);
                }


                // Guid selectedID = (Guid)dgEarlyWarningList.Rows[index].Cells[0].Value;

                if (!flag)
                {
                    earlyWarning = earlyWarningLst.Find(s => s.EarlyWarningID == selectedID);
                    if (earlyWarning == null) earlyWarning = new EarlyWarning();
                    if (earlyWarning.Airport == "0000") { btnDelete.Enabled = false; btnCreate.Enabled = true; btnEdit.Enabled = true; txtAirPortCode.Enabled = false; } else { setControlsEnabled(true); btnDelete.Enabled = true; btnCreate.Enabled = true; btnEdit.Enabled = true; txtAirPortCode.Enabled = true; }
                }


            }

            if (!flag)
            {
                txtAirPortCode.Text = (earlyWarning.Airport == "0000") ? "通用规则机场" : earlyWarning.Airport;
                txtAOCEarlyTime.Text = earlyWarning.AOCEarlyTime.ToString();
                txtSMSEarlyTime.Text = earlyWarning.SMSEarlyTime.ToString();
            }
        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgEarlyWarningList.CurrentRow.Index < 0 || dgEarlyWarningList.CurrentRow.Index > earlyWarningLst.Count - 1)
            {
                MessageBox.Show("请选择一条警告规则！");
                return;
            }
            if (MessageBox.Show("确认删除此记录？", "删除确认", MessageBoxButtons.OKCancel) != DialogResult.OK)
            {
                return;
            }
            Guid EarlyWarningID = new Guid((dgEarlyWarningList.CurrentRow.Cells["EarlyWarningID"].Value).ToString());
            try
            {
                if (service.DeleteEarlyWarning(EarlyWarningID))
                {
                    MessageBox.Show("删除成功");
                    BindDataGrid();

                    dgEarlyWarningList.Rows[0].Selected = true;
                    dgEarlyWarningList.CurrentCell = dgEarlyWarningList.Rows[0].Cells[1];
                    dgEarlyWarningList_SelectionChanged(null, null);

                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
            catch (SoapHeaderException soapEx)
            {
                MessageBox.Show("SoapHeader Error : " + soapEx.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            grpEarlyWarningInfo.Visible = false;
            if (btnSave.Tag.ToString() == "create")
            {
                dgEarlyWarningList.Rows[0].Selected = true;
                dgEarlyWarningList.CurrentCell = dgEarlyWarningList.Rows[0].Cells[1];
            }
            else if (btnSave.Tag.ToString() == "alter")
            {
            }
            dgEarlyWarningList_SelectionChanged(null, null);
            setControlsEnabled(false);

            btnEdit.Enabled = true;
            btnDelete.Enabled = true;
            btnCreate.Enabled = true;
        }

        private void dgEarlyWarningList_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            previewIndex = e.RowIndex;
        }

        private void txtAOCEarlyTime_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8) && (e.KeyChar != 13))
            {
                e.Handled = true;
                base.OnKeyPress(e);
                MessageBox.Show("输入必须是数字！", "提示");
            }

            //int dd = int.Parse("033");
            //if (int.Parse((sender as TextBox).Text) > 255) MessageBox.Show("数字太大！","提示");
        }

    }
}
